//
//  MainTabBar.swift
//  vveboSwiftPro
//
//  Created by 杨笑怡 on 15/6/28.
//  Copyright © 2015年 YangXiaoYi. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    // 按钮总数
    private let buttonCount = 5

    // 当系统的布局方法不能满足需求时，在此方法中调整布局
    // 因为会被反复调用，所以不要写太耗时的代码
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = bounds.size.width / CGFloat(buttonCount)
        let rect = CGRectMake(0, 0, w, bounds.size.height)
        
        // UITabBarButton 是私有接口，在开发中不要使用
        // 遍历所有子视图
        var index = 0
        for v in subviews {
            // 判断控件
            if v is UIControl && !(v is UIButton) {
                v.frame = CGRectOffset(rect, CGFloat(index) * w, 0)
                index += index == 1 ? 2 : 1
            }
        }
        // 设置加号按钮位置
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    lazy var composeButton: UIButton = {
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // self. 闭包，提前准备好的代码，在需要的时候执行
        self.addSubview(btn)
        return btn
    }()

}
